package cn.hdy.base.common;

import cn.hdy.base.exception.BusinessException;
import cn.hdy.base.util.AjaxResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
//全局异常类
@RestControllerAdvice
public class GlobalExceptionHandler {
    //捕获业务异常
    //捕捉BusinessException异常: 应用到所有@RequestMapping的方法上，在其抛出BusinessException时执行
    @ExceptionHandler(BusinessException.class)
    public AjaxResult businessException(BusinessException e){
        e.printStackTrace();
        return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
    }
    //捕获非业务异常
    //捕捉其他异常
    @ExceptionHandler(Exception.class)
    public AjaxResult exception(Exception e){
        e.printStackTrace();
        return AjaxResult.me().setSuccess(false).setMessage("网络繁忙，请稍后重试");
    }
}
